<?php
/**
 * @package	minBot
 * @author	Martin Sadový <sodae@email.cz>
 * @copyright   2009 Martin Sadový
 */
 
class Message extends Object
{
    /**
     * Data from original message
     * @var array
     */
    private $_data;

    /**
     * Jid from presence(=available)
     * @var string
     */
    protected $jid = NULL;

    public function __construct($data)
    {
        $this->_data = $data;

        if(!$this->isInRoom()){
            $explode = explode('/', $data['from']);
            $this->jid = $explode[0];
        }
        
    }

    /**
     * Set Jid
     * @param string $jid
     */
    public function setJid($jid)
    {
        $this->jid = $jid;
    }

    /**
     * Get type of message, is in chat or groupchat
     * @return string Type of Message
     */
    public function getType()
    {
        return $this->_data['type'];
    }

    /**
     * Get sender message
     * @return string Type of from
     */
    public function getFrom()
    {
        return $this->_data['from'];
    }

    /**
     * Get body of message
     * @return string Type of from
     */
    public function getBody()
    {
        return $this->_data['body'];
    }

    /**
     * Get jid of sender message
     * @return string Type of from
     */
    public function getJid()
    {
        return $this->jid;
    }

    /**
     * Is message from room (groupchat)
     * @return bool
     */
    public function isInRoom()
    {
        return ($this->_data['type'] == 'groupchat');
    }

    /**
     * Is message from private chat
     * @return bool
     */
    public function isChat()
    {
        return ($this->_data['type'] == 'chat');
    }

    /**
     * Get time of adoption of the report
     * @return int UNIXTIME
     */
    public function getTime($time = false)
    {
        $i = 0;
        while(true){
            $i++;
            if(isset($this->_data['xml']->subs[$i]->attrs['stamp'])){
                return $this->convertTime($this->_data['xml']->subs[$i]->attrs['stamp']);
            }elseif($i == 10 AND $time){
                return NULL;
            }elseif($i == 10){
                return time();
            }
        }
    }

    /**
     * Time of message
     * @param string $date
     * @return int UNIXTIME
     */
    private function convertTime($date)
    {
        return empty($date) ? time() : strtotime($date);
    }

}
?>